<?php

//<editor-fold defaultstate="collapsed" desc="License & Copyright">
/*
 * @license
 */

/**
 *
 * @author Jevgeni Geimanen (jevgeni.geimanen@gmail.com)
 */
//</editor-fold>
class SCUITemplater {

    //<editor-fold defaultstate="collapsed" desc="Smarty">
    /**
     * @var Smarty
     */
    private static $_Smarty = null;

    /**
     *
     * @return Smarty
     */
    public static function getSmarty() {
        if (self::$_Smarty === null) {
            self::$_Smarty = new Smarty();
            self::$_Smarty->setCompileDir(SCORE_APP_VAR_PATH);
            self::$_Smarty->setPluginsDir(SCORE_PATH . "plugins/smarty/plugins/");
        }
        return self::$_Smarty;
    }

    //</editor-fold>

    private static $_CurrentComponents = array();

    public static function RenderComponent(Component $component) {
        if (($relative = $component->getRelativePath()) != null) {
            if ($relative = preg_replace("/\.php$/", ".html", $relative)) {
                $paths = array(
                    SCORE_APP_LIB_PATH . $relative,
                    SCORE_PATH . $relative
                );
                self::getSmarty()->assign('site',Site::getInstance());
                foreach ($paths as $filename) {
                    if (file_exists($filename)) {
                        $vars = self::CollectFlaggedProperties($component, "smarty");
                        $vars['this'] = $component;
                        foreach ($vars as $name => $var) {
                            self::getSmarty()->assign($name, $var);
                            self::$_CurrentComponents[$name][] = $var;
                        }
                        $res = self::getSmarty()->fetch($filename);
                        foreach ($vars as $name => $var) {
                            array_pop(self::$_CurrentComponents[$name]);
                            if (count(self::$_CurrentComponents[$name]) > 0) {
                                self::getSmarty()->assign($name, self::$_CurrentComponents[$name][count(self::$_CurrentComponents[$name]) - 1]);
                            } else {
                                self::getSmarty()->assign($name, null);
                            }
                        }
                        return $res;
                    }
                }
            }
        }
    }

    /**
     *
     * @TODO Move to better place
     * @TODO Do caching
     * @param ReflectionClass $classRef
     * @param string $flag
     * @return array 
     */
    private static function CollectFlaggedProperties(SCStruct $class, $flag) {
        $names = self::CollectFlaggedPropertiesNames($class,$flag);
        $res = array();
        foreach ($names as $name=>$key)
            $res[$name] = $class->{$key}();
        return $res;
    }
    
/**
     *
     * @param ReflectionClass $classRef
     * @param string $flag
     * @return array 
     */
    private static function CollectFlaggedPropertiesNames(SCStruct $class, $flag) {
        if (($res = SCCacheHelper::getInstance()->getObject($cacheKey = __FUNCTION__ . get_class($class) . $flag)) === false || IS_DEV) {
            $res = array();
            $refClass = $class->getRefClass();
            while ($refClass !== false) {
                $methods = $refClass->getMethods(ReflectionMethod::IS_PUBLIC);
                $matches = array();
                foreach ($methods as $method) {
                    if (preg_match("/@{$flag}\s+?(.*?)(\s|$)/", $method->getDocComment(), $matches)) {
                        $res[$matches[1]] = $method->getName();
                    }
                }
                $refClass = $refClass->getParentClass();
            }
            SCCacheHelper::getInstance()->setObject($cacheKey, $res);
        }
        return $res;
    }

}

?>
